package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootUserFormation;
import org.footballmanager.pandafootball.system.form.impl.FootUserFormationSearchForm;
import org.footballmanager.pandafootball.system.form.impl.FootZjSearchForm;
import org.footballmanager.pandafootball.system.service.FootFormationService;
import org.footballmanager.pandafootball.system.service.FootUserFormationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SessionAttributes("footUserFormationSearchForm")
@RequestMapping("/hy/footUserFormation")
public class HyFormationController extends HyAddAndEditController<FootUserFormation> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footUserFormation";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.HY, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.HY, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.HY, ENTITYNAME);

    @Autowired
    private FootFormationService footFormationService;
    @Autowired
    private FootUserFormationService footUserFormationService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                         参数
     * @param footUserFormationSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootUserFormationSearchForm footUserFormationSearchForm) {
        map.addAttribute("formationList", footFormationService.getall());
        FootUserFormationSearchForm newFootUserFormationSearchForm = new FootUserFormationSearchForm();
        if (footUserFormationSearchForm != null) {
            newFootUserFormationSearchForm = footUserFormationSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootUserFormationSearchForm);
    }

    /**
     * 系统建议意见列表
     *
     * @param map                         参数
     * @param footUserFormationSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/search")
    public String search(Model map, HttpServletRequest request, @ModelAttribute FootUserFormationSearchForm footUserFormationSearchForm) {
        footUserFormationSearchForm.setUserId((Long) request.getSession().getAttribute(Constants.USERID));
        footUserFormationService.search(ENTITYNAME, map, footUserFormationSearchForm);
        return LISTPAGE;
    }

    /**
     * 删除系统建议意见记录
     *
     * @param id      主键
     * @param request 请求
     * @return 页面
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footUserFormationService.delete(id, request);
        return REDIRECT;
    }

    /**
     * 删除系统建议意见列表
     *
     * @param request                     请求
     * @param footUserFormationSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootUserFormationSearchForm footUserFormationSearchForm) {
        footUserFormationService.deleteAll(footUserFormationSearchForm, request);
        return REDIRECT;
    }

    /**
     * 打开添加系统建议意见页面
     *
     * @param map 参数
     * @return 页面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addAddPage(Model map) {
        map.addAttribute(ENTITYNAME, new FootUserFormation());
        return ADDPAGE;
    }

    /**
     * 打开编辑系统建议意见页面
     *
     * @param map 参数
     * @param id  主键
     * @return 页面
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footUserFormationService.get(id));
        return EDITPAGE;
    }

    /**
     * 浏览系统建议意见页面
     *
     * @param map                         参数
     * @param id                          主键
     * @param footUserFormationSearchForm 条件
     * @return 页面
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootUserFormationSearchForm footUserFormationSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footUserFormationSearchForm);
        map.addAttribute(ENTITYNAME, footUserFormationService.get(id));
        return VIEWPAGE;
    }

    @RequestMapping(value = "/formation")
    public String formation(Model map, HttpServletRequest request,
                            @ModelAttribute FootZjSearchForm footZjSearchForm) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        FootUserFormation formation = footUserFormationService.findByUserId(userId);
        map.addAttribute("formation", formation);
        return "footUserFormation/Formation";
    }

    @RequestMapping(value = "/startup")
    public String startup(@RequestParam Long id, Model map, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        List<FootUserFormation> formations = footUserFormationService.findByUserIdWithoutStatus(userId);
        if (formations == null || formations.isEmpty()) {
            return REDIRECT;
        }
        for (FootUserFormation formation : formations) {
            formation.setStatus(1);
            footUserFormationService.updateNotNull(formation, request);
            if (formation.getId() == id) {
                formation.setStatus(0);
                footUserFormationService.updateNotNull(formation, request);
            }
        }
        return REDIRECT;
    }

}
